package cn.cvs.eduservice.service.impl;

import cn.cvs.baseservice.exception.LingQiException;
import cn.cvs.baseservice.utils.ResultCode;
import cn.cvs.eduservice.entity.EduCourse;
import cn.cvs.eduservice.entity.EduCourseDescription;
import cn.cvs.eduservice.entity.vo.CourseInfoVo;
import cn.cvs.eduservice.entity.vo.CourseQuery;
import cn.cvs.eduservice.mapper.EduCourseMapper;
import cn.cvs.eduservice.service.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;


    @Override
    @Transactional
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //保存课程基本信息
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int result = baseMapper.insert(eduCourse);
        if(result == 0){
            throw new LingQiException(ResultCode.ERROR, "课程信息保存失败");
        }
        //保存课程详情信息
        EduCourseDescription eduCourseDescription=new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(eduCourse.getId());
        boolean flag = eduCourseDescriptionService.save(eduCourseDescription);
        if(!flag) {
            throw new LingQiException(ResultCode.ERROR, "课程信息保存失败");
        }
        return eduCourse.getId();
    }

    @Override
    public CourseInfoVo getCourseInfoVoById(String id) {
        EduCourse course = this.getById(id);
        if(course == null){
            throw new LingQiException(ResultCode.ERROR, "数据不存在");
        }
        CourseInfoVo courseInfoVo=new CourseInfoVo();
        BeanUtils.copyProperties(course,courseInfoVo);
        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(id);
        courseInfoVo.setDescription(eduCourseDescription.getDescription());
        return courseInfoVo;
    }

    @Override
    @Transactional(rollbackFor = LingQiException.class)
    public void updateCourseInfo(CourseInfoVo courseInfoVo){
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int result = baseMapper.updateById(eduCourse);
        if(result<=0){
            throw new LingQiException(ResultCode.ERROR,"修改课程失败");
        }
        EduCourseDescription eduCourseDescription=new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo,eduCourseDescription);
        boolean flag = eduCourseDescriptionService.updateById(eduCourseDescription);
        if(!flag){
            throw new LingQiException(ResultCode.ERROR,"修改课程失败");
        }
    }
    @Override
    public Page<CourseInfoVo> pageQuery(Page<CourseInfoVo> pageParam, CourseQuery courseQuery) {
        return pageParam.setRecords(baseMapper.getCourseVo(pageParam,courseQuery));
    }

    @Override
    @Transactional(rollbackFor = LingQiException.class)
    public boolean removeCourseById(String id) {
        //删除课程简介
        boolean flag = eduCourseDescriptionService.removeById(id);
        if(!flag){
            throw new LingQiException(ResultCode.ERROR,"删除课程失败");
        }
        //删除课程
        int result = baseMapper.deleteById(id);
        if(result<=0){
            throw new LingQiException(ResultCode.ERROR,"删除课程失败");
        }
        return true;
    }
}